const db = wx.cloud.database();
let that = this;
const app = getApp();

Page({
  data: {
    user_nickname: '',
    user_url: '',
    openid: '',

    title: '',
    description: '',
    image: '',
    number: 1 //图片数
  },

  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false

      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      items
    })
  },
  // 类型选择
  checkboxChange(e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)

    const items = this.data.items
    const values = e.detail.value
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].value === values[j]) {
          items[i].checked = true
          break
        }
      }
    }
    this.setData({
      items
    })
  },

  onLoad() {
    let userinfo = wx.getStorageSync('app.globalData.userInfo');
    let openid = wx.getStorageSync('app.globalData.userOpenId');
    console.log(userinfo)
    this.setData({
      user_nickName: userinfo.nickName,
      user_url: userinfo.avatarUrl,
      openid: openid
    })
  },

  onReady() {},

  onShow() {},

  

  addImage: function (e) {
    var that = this
    wx.chooseMedia({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        console.log("成功", res);
        that.uploadImage(res.tempFiles[0].tempFilePath);
        that.setData({
          image: res.tempFiles[0].tempFilePath,
          number: res.tempFiles.length + 1
        })
      }
    })
  },

  uploadImage(fileURL) {
    var that = this
    wx.cloud.uploadFile({
      cloudPath: "userpost/" + new Date().getTime() + '.png',
      filePath: fileURL,
      success: res => {
        that.setData({
          image:res.fileID
        })
      },
      fail: console.error
    })
  },

  btnsub: function (res) {
    var that = this
    console.log(res)
    var description = res.detail.value.description
    var image = that.data.image
    var title=that.data.title

    var usrn = that.data.user_nickName
    var usri = that.data.user_url
    var openid = that.data.openid

    var tempFilePaths = that.data.Filepath
    if (title!="") {
      wx.showLoading({
        title: '发布中...',
        mask: true,
        duration: 1000
      }).then(res => {
        db.collection("post").add({
          data: {
            user_nickname: usrn,
            user_url: usri,
            openid: openid,

            title:title,
            description: description,
            image: image
          }
        }).then(res => {
          wx.hideLoading()
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 1000,
            success: function () {
              console.log(res)
            }
          })
          that.setData({
            user_nickname: '',
            user_url: '',
            openid: '',

            tilte: '',
            description: '',
            image: '',
            number: 1 //图片数
          })
          wx.switchTab({
            url: '../home/home'
          })
        }, )
      })
      fail: console.error();
    } else {
      wx.showToast({
        icon: 'none',
        title: '请检查输入的数据是否有误！',
        duration: 1000,
        mask: true,
      })
    }
  },

  cancel: function () {
    wx.navigateTo({
      url: '../../pages/home'
    })
  },
})